﻿using Shop.Implementation.Customer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WxApi.ApiFunction;
using WxApi.ApiResult;

namespace Shop
{
    public partial class wxPTPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // var menus = wxFunMenuApi.getMenu();
            //校验数据
            string signature = Request.QueryString["signature"];
            string timestamp = Request.QueryString["timestamp"];
            string nonce = Request.QueryString["nonce"];
            bool check = wxValidApi.CheckSignature(signature, timestamp, nonce);
            if (!check)
            {
                System.Reflection.MethodBase.GetCurrentMethod().WriteErrLog("验证消息真实性---失败！", "WEIXIN");
                Response.Write(string.Empty);
                Response.End();
                return;
            }

            var result = wxResultApi.WxResult(Request.InputStream);
            var tmp = string.Empty;
            string returnStr = string.Empty;
            tmp = Common.HandleCookie.GetCookieValue("UseOpenId");
            if (tmp.Length > 0)
                Common.HandleCookie.ClearCookie("UseOpenId");
            Common.HandleCookie.SetCookie("UseOpenId", result.FromUserName);

            var getvalue = Common.HandleCookie.GetCookieValue("openid");
            if (getvalue.Length > 0) Common.HandleCookie.ClearCookie("openid");
            Common.HandleCookie.SetCookie("openid", result.FromUserName);//["openid"] = openid;

            if (result.MsgType == "event")
            {
                if (result.EventKey == "KEY_KF")
                {
                    returnStr = WxReturnApi.ReturnText(result, "客服电话：4008-237-007\n受理时间：周一至周日8：00-20:00\n认真聆听您的声音~~");
                }
                if (result.Event == "subscribe")
                {
                    returnStr = WxReturnApi.ReturnText(result, "欢迎您关注王品美厨公众号!");
                    string scene_id = result.scene_id;

                    if (scene_id.Length > 0)
                    {
                        StringBuilder sql = new StringBuilder();
                        sql.Append(string.Format(@"INSERT INTO PersonOpenId (openId,IdPerson,dDate) VALUES('{0}',{1},GETDATE())", result.FromUserName, scene_id));
                        Common.HandleSQL.getObj(sql.ToString());
                    }
                    System.Reflection.MethodBase.GetCurrentMethod().WriteErrLog("IDPerson:" + scene_id, "WEIXINIDPERSON");
                }
            }

            System.Reflection.MethodBase.GetCurrentMethod().WriteErrLog("UseOpenId:" + result.FromUserName, "WEIXIN");
            // System.Reflection.MethodBase.GetCurrentMethod().WriteErrLog("EventKey:" + result.EventKey, "WEIXIN");
            Response.Write(returnStr);
            Response.End();
        }
        #region 菜单
        private void menu()
        {
            #region  获取菜单
            //wxFunMenuApi.delMenu();
            var menus = wxFunMenuApi.getMenu();
            //          //  if (menus.Length == 0)
            //          //  {
            //                string postStr = "";
            //                postStr = @" {""button"":[
            //{""type"":""view"",  ""name"":""商城"",""url"":""https://www.wpmeichu.com/DefaultPage1.aspx""},  
            //{""type"":""view"",""name"":""我要注册"",""url"":""https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx756e0dc6491321bd&redirect_uri=https://www.wpmeichu.com/DefaultPage2.aspx&response_type=code&scope=snsapi_base&state=1#wechat_redirect"" },  
            //{""name"":""关于美厨"",""sub_button"":[  
            //{""type"":""view"", ""name"":""公司介绍"",""url"":""http://mp.weixin.qq.com/s/qJZWWSMHNc5DEFHOkGV94w""},
            //{""type"":""view"",""name"":""售后规则"",""url"":""https://www.wpmeichu.com/DefaultPage3.aspx""},
            //{""type"":""click"",""name"":""联系客服"",""key"":""KEY_KF""},
            //{""type"":""view"", ""name"":""红包规则"",""url"":""https://www.wpmeichu.com/wangpin0919/CouponIndex.html""},
            //{""type"":""view"", ""name"":""内部测试"",""url"":""https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx756e0dc6491321bd&redirect_uri=https://www.wpmeichu.com/wxPage.aspx&response_type=code&scope=snsapi_base&state=1#wechat_redirect""  }
            //]}]  }";
            //                wxFunMenuApi.CreateMenu(postStr);
            //          //  }
            #endregion
        }
        #endregion
    }
}